package com.wj.dubbo.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {
    
    public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    
    public static String format(Date date, String pattern){
        dateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
        return dateFormat.format(date);
    }
    
    /**
     * 计算月份差值
     * @author wangjue
     * @date 2016年1月11日上午11:07:21
     *
     * @param date1 起始时间
     * @param date2 结束时间
     * @return
     * @throws ParseException
     */
    public static String getMonthSpace(String date1, String date2) throws ParseException {
        String str="";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));
        int yearResult=c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        int result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        if (yearResult<=0) {
			if (result>0) {
				str=result+"个月";
			}else if (result==0) {
				str="1个月";
			}
		}else{
			if (result>0) {
				str=yearResult+"年"+result+"个月";
			}
			else if (result==0) {
				str=yearResult+"年";
			}else{
				str=((yearResult-1)==0?"":(yearResult+"年"))+(result+12)+"个月";
			}
		}
        return str;

    }
    public static String getMonthSpace(Date begin, Date end) throws ParseException {
    	 String str="";
         Calendar c1 = Calendar.getInstance();
         Calendar c2 = Calendar.getInstance();
         c1.setTime(begin);
         c2.setTime(end);
         int yearResult=c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
         int result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
         if (yearResult<=0) {
 			if (result>0) {
 				str=result+"个月";
 			}else if (result==0) {
 				str="1个月";
 			}
 		}else{
 			if (result>0) {
 				str=yearResult+"年"+result+"个月";
 			}
 			else if (result==0) {
 				str=yearResult+"年";
 			}else{
 				str=((yearResult-1)==0?"":(yearResult-1+"年"))+(result+12)+"个月";
 			}
 		}
         return str;

    }
    
    public static String getLastMonth() throws Exception{
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-");
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(new Date());
         calendar.add(Calendar.MONTH, -1);
         Date time = calendar.getTime();
         return sdf.format(time);
    }
    
    
	/**
	 * 获取上一个月
	 * 
	 * @author wangjue
	 * @date 2016年4月29日下午4:26:53
	 * 
	 * @param str 表达式
	 * @return
	 * @throws Exception
	 */
	public static String getPreMonth(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		Date time = calendar.getTime();
		return sdf.format(time);
	}
	
	/**
	 * 获取上一个月
	 * 
	 * @author wangjue
	 * @date 2016年4月29日下午4:26:53
	 * 
	 * @param str 表达式
	 * @param month 减月
	 * @return
	 * @throws Exception
	 */
	public static String getSubMonth(String str,int month) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -month);
		Date time = calendar.getTime();
		return sdf.format(time);
	}
	
	/**
	 * 最近缴纳前4个月
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static int getCrawlYear(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date=sdf.parse(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -9);//-12+2+1
		return calendar.get(Calendar.YEAR);
	}
	
	/**
	 * 取当前年份
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static int getNowYear() throws Exception {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.YEAR);
	}
	
	public static void main(String[] args) throws Exception {
//			System.out.println(getMonthSpace(new Date(),new Date()));
//	    	System.out.println(getPreMonth("yyyyMM"));
		System.out.println(getNowYear());
		System.out.println(DateUtils.getSubMonth("yyyy-MM",-1));
		System.out.println(DateUtils.getCrawlYear("2017-03"));
	}
}
